package com.longrise.android.widget;

import com.longrise.android.util.Util;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class ViewPagerIndicator extends View {

	private Context context = null;
	//图标的绘制画笔对象
	private Paint mPaintPageFillAndStroke;
	//滑动图标绘制画笔对象
    private Paint mPaintPageFill;
    //图标的个数
    private int count = 3;
	//图标的半径
    private int mRadius = 5;
    //页面滑动百分比
    private float mPageOffset = 0;
    //当前页面下标值
    private int mCurrentPage = 0;
    private int mSnapPage = 0;
    
	public ViewPagerIndicator(Context context)
	{
		super(context);
		this.context = context;
		initView();
	}
	public ViewPagerIndicator(Context context, AttributeSet attrs) 
	{
		super(context, attrs);
		this.context = context;
		initView();
	}
	public ViewPagerIndicator(Context context, AttributeSet attrs, int defStyle) 
	{
		super(context, attrs, defStyle);
		this.context = context;
		initView();
	}
	
	//========================================set()/get()=================================
	public void setCount(int count)
	{
		this.count = count;
		invalidate();
	}
	public void setmRadius(int mRadius) 
	{
		this.mRadius = Util.dip2px(context, mRadius);
		invalidate();
	}
	 //图标的颜色
	public void setIconColor(int iconColor) 
	{
				
		// 设置颜色
		if (null != mPaintPageFillAndStroke)
		{
			mPaintPageFillAndStroke.setColor(iconColor);
		}
		invalidate();
	}
	//滑动图标的颜色
	public void setmIconColor(int mIconColor)
	{
		if(null != mPaintPageFill)
		{
			mPaintPageFill.setColor(mIconColor);
		}
		invalidate();
	}
    //图片的绘制风格
	public void setIconStyle(Paint.Style iconStyle)
	{
		if(null != mPaintPageFillAndStroke)
		{
			mPaintPageFillAndStroke.setStyle(iconStyle);
		}
		invalidate();
	}
    //滑动图标的绘制风格
	public void setmIconStyle(Paint.Style mIconStyle)
	{
		if(null != mPaintPageFill)
		{
			mPaintPageFill.setStyle(mIconStyle);
		}
		invalidate();
	}
	
	
	private void initView()
	{
		mPaintPageFillAndStroke = new Paint();
		//设置为实心
		mPaintPageFillAndStroke.setStyle(Paint.Style.FILL_AND_STROKE);
		//去掉锯齿
		mPaintPageFillAndStroke.setAntiAlias(true);
		//设置颜色
		mPaintPageFillAndStroke.setColor(Color.GRAY);
		
		mPaintPageFill = new Paint();
		mPaintPageFill.setStyle(Paint.Style.FILL);
		mPaintPageFill.setAntiAlias(true);
		mPaintPageFill.setColor(Color.LTGRAY);
	}
	
	@Override
    protected void onDraw(Canvas canvas) 
	{
        super.onDraw(canvas);

        if(count <= 1 || mRadius <= 0)
        {
        	return;
        }
        int longSize;
        int longPaddingBefore;
        int longPaddingAfter;
        int shortPaddingBefore;
        
        longSize = getWidth();
        longPaddingBefore = getPaddingLeft();
        longPaddingAfter = getPaddingRight();
        shortPaddingBefore = getPaddingTop();
        
        float threeRadius = mRadius * 3;
        float shortOffset = shortPaddingBefore + mRadius;
        float longOffset = longPaddingBefore + mRadius;
        
        longOffset += ((longSize - longPaddingBefore - longPaddingAfter) / 2.0f) - ((count * threeRadius) / 2.0f);
        
        float dX;
        float dY;
        
        //Draw stroked circles
        for (int iLoop = 0; iLoop < count; iLoop++) 
        {
        	float drawLong = longOffset + (iLoop * threeRadius);
        	dX = drawLong;
            dY = shortOffset;
            canvas.drawCircle(dX, dY, mRadius, mPaintPageFillAndStroke);
        }

        boolean mSnap = false;
        float cx = (mSnap ? mSnapPage : mCurrentPage) * threeRadius;
        if (!mSnap) 
        {
            cx += mPageOffset * threeRadius;
        }
        dX = longOffset + cx;
        dY = shortOffset;
        canvas.drawCircle(dX, dY, mRadius, mPaintPageFill);
    }
	
	/**
	 * 滑动时回调方法
	 * @param mCurrentPage
	 * 			滑动时的页码
	 * @param positionOffset
	 * 			页面滑动的百分比
	 */
	public void setViewPagerChange(int mCurrentPage,float positionOffset)
	{
		if(mCurrentPage < 0 || mCurrentPage >= count)
		{
			return;
		}
		this.mCurrentPage = mCurrentPage;
		this.mPageOffset = positionOffset;
		invalidate();
	}
	
	/**
	 * 设置选中某一个图标
	 * @param currentPage
	 * 			下标值
	 */
	public void setViewPagerSelect(int currentPage )
	{
		if(currentPage < 0 || currentPage >= count)
		{
			return;
		}
		mCurrentPage = currentPage;
        mSnapPage = currentPage;
        invalidate();
	}
	
	/**
	 * 重写该方法计算视图的宽和高
	 */
	@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
	{
		setMeasuredDimension(measureLong(widthMeasureSpec), measureShort(heightMeasureSpec));
    }
	
	/**
	 * 计算宽度
	 * @param measureSpec
	 * @return
	 */
	private int measureLong(int measureSpec) 
	{
        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        //如果是充满父控件时直接返回获取到的大小
        if ((specMode == MeasureSpec.EXACTLY) || (count <= 0)) 
        {
            result = specSize;
        }
        else
        {
        	//如果是自适应则计算宽度
            result = (int)(getPaddingLeft() + getPaddingRight()
                    + (count - 1) * mRadius * 3 + count * mRadius);
            if (specMode == MeasureSpec.AT_MOST) 
            {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }
	/**
     * 计算高度
     */
    private int measureShort(int measureSpec) 
    {
        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) 
        {
            result = specSize;
        } 
        else 
        {
            result = (int)(2 * mRadius + getPaddingTop() + getPaddingBottom() + 1);
            if (specMode == MeasureSpec.AT_MOST) 
            {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }
}
